import type { StateCreator } from 'zustand';
import type { SystemStore } from '../index';

// 主题类型
export type Theme = 'light' | 'dark' | 'auto';

// 语言类型
export type Language = 'zh-CN' | 'en-US' | 'ja-JP';

// 菜单标签类型
export type ActiveTab = 'chat' | 'market';

// 系统状态接口
export interface SystemState {
  theme: Theme;
  language: Language;
  isLoading: boolean;
  error: string | null;
  sidebarCollapsed: boolean;
  mobileMenuOpen: boolean;
  activeTab: ActiveTab;
}

// 系统Actions接口
export interface SystemActions {
  // 主题相关
  setTheme: (theme: Theme) => void;
  toggleTheme: () => void;
  
  // 语言相关
  setLanguage: (language: Language) => void;
  
  // 加载状态
  setLoading: (loading: boolean) => void;
  
  // 错误处理
  setError: (error: string | null) => void;
  clearError: () => void;
  
  // UI状态
  setSidebarCollapsed: (collapsed: boolean) => void;
  toggleSidebar: () => void;
  setMobileMenuOpen: (open: boolean) => void;
  toggleMobileMenu: () => void;
  
  // 菜单标签
  setActiveTab: (tab: ActiveTab) => void;
  
  // 重置状态
  resetSystemState: () => void;
}

// 系统Slice类型
export type SystemSlice = SystemState & SystemActions;

// 默认状态
const initialSystemState: SystemState = {
  theme: 'auto',
  language: 'zh-CN',
  isLoading: false,
  error: null,
  sidebarCollapsed: false,
  mobileMenuOpen: false,
  activeTab: 'chat',
};

// 创建系统Slice
export const createSystemSlice: StateCreator<
  SystemStore,
  [['zustand/immer', never], ['zustand/devtools', never], ['zustand/persist', unknown], ['zustand/subscribeWithSelector', never]],
  [],
  SystemSlice
> = (set) => ({
  // 初始状态
  ...initialSystemState,

  // Actions
  setTheme: (theme) => {
    set((state: SystemStore) => {
      state.theme = theme;
    });
  },

  toggleTheme: () => {
    set((state: SystemStore) => {
      if (state.theme === 'light') {
        state.theme = 'dark';
      } else if (state.theme === 'dark') {
        state.theme = 'light';
      } else {
        // auto状态下切换到light
        state.theme = 'light';
      }
    });
  },

  setLanguage: (language) => {
    set((state: SystemStore) => {
      state.language = language;
    });
  },

  setLoading: (loading) => {
    set((state: SystemStore) => {
      state.isLoading = loading;
    });
  },

  setError: (error) => {
    set((state: SystemStore) => {
      state.error = error;
    });
  },

  clearError: () => {
    set((state: SystemStore) => {
      state.error = null;
    });
  },

  setSidebarCollapsed: (collapsed) => {
    set((state: SystemStore) => {
      state.sidebarCollapsed = collapsed;
    });
  },

  toggleSidebar: () => {
    set((state: SystemStore) => {
      state.sidebarCollapsed = !state.sidebarCollapsed;
    });
  },

  setMobileMenuOpen: (open) => {
    set((state: SystemStore) => {
      state.mobileMenuOpen = open;
    });
  },

  toggleMobileMenu: () => {
    set((state: SystemStore) => {
      state.mobileMenuOpen = !state.mobileMenuOpen;
    });
  },

  setActiveTab: (tab) => {
    set((state: SystemStore) => {
      state.activeTab = tab;
    });
  },

  resetSystemState: () => {
    set((state: SystemStore) => {
      Object.assign(state, initialSystemState);
    });
  },
}); 